package com.example.demo.modules.demo.rsocket;

import com.example.demo.common.r2dbc.BaseController;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.rsocket.annotation.support.RSocketFrameTypeMessageCondition;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.LocalDateTime;

@Controller
@MessageMapping("/rsdemo")
public class RsDemoController extends BaseController {

    private static final String RSOCKET_FRAME_TYPE = RSocketFrameTypeMessageCondition.FRAME_TYPE_HEADER;
    private static final String CONTENT_TYPE = "contentType";

    @MessageMapping("request-response")
    public Mono<String> reqResponse(@Payload String payload) {
        log.info("收到 RR 请求信息: {}", payload);
        return Mono.just("Hello, " + payload);
    }

    @MessageMapping("rsStream")
    public Flux<String> rsStream(@Header(RSOCKET_FRAME_TYPE) String rsocketFrameType,
                          @Header(CONTENT_TYPE) String contentType,
                          @Payload String payload) {
        return Flux.interval(Duration.ofSeconds(1)).map(aLong -> payload + LocalDateTime.now());
    }
}
